package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.Dish;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 王磊
 * @date 2022/5/11
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 查询购物车数据
     * 查看当前用户的购物车,并不是查看购物车表中的所有数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 移除购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        lambdaQueryWrapper.eq(shoppingCart.getDishId()!=null, ShoppingCart::getDishId, shoppingCart.getDishId());
        ShoppingCart cart = shoppingCartService.getOne(lambdaQueryWrapper);
        Integer number = cart.getNumber();
        if (number > 1) {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
        } else {
            shoppingCartService.removeById(cart);
        }
        return R.success("移除购物车成功");
    }

    /**
     * 添加购物车
     * 要区分添加的菜品还是套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //获取当前用户的id,设置到购物车数据里面,
        Long userID = BaseContext.getCurrentId();
        shoppingCart.setUserId(userID);

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //获取菜品id
        Long dishId = shoppingCart.getDishId();

        wrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        //如果添加的是菜品
        if (dishId!=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //否则添加的就是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //获取到当前购物车的数据
        ShoppingCart cart = shoppingCartService.getOne(wrapper);

        if (cart!=null){
            Integer number = cart.getNumber();
            cart.setNumber(number+1);
            shoppingCartService.updateById(cart);
        }else {
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart=shoppingCart;
        }


        /*//首先查询当前用户的购物车信息
        List<ShoppingCart> list = shoppingCartService.list();
        //遍历这个list,如果当前菜品或套餐已经存在,那么久改变数量即可,
        if (list.size()>0){
            for (ShoppingCart item : list) {
                    LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(item.getDishId()!=null,ShoppingCart::getDishId, item.getDishId());
                    wrapper.eq(item.getSetmealId()!=null,ShoppingCart::getSetmealId,item.getSetmealId());
                    ShoppingCart cart = shoppingCartService.getOne(wrapper);
                    if (cart.getNumber() > 0) {
                        cart.setNumber(cart.getNumber() + 1);
                        shoppingCartService.updateById(cart);
                    } else {
                        shoppingCart.setUserId(BaseContext.getCurrentId());
                        shoppingCartService.save(shoppingCart);
                    }
                }
        }else {
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartService.save(shoppingCart);
        }*/
        //如果添加的是套餐
        return R.success(cart);
    }

    /**
     * 清空购物车
     * 是清除当前用户的购物车,并不是把所有购物车的数据都清了哦
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清除成功");
    }
}
